//
//  HNAPayMobileSDKManager.m
//  HNAPayMobileSDK
//
//  Created by Ly_work on 15/11/18.
//  Copyright © 2015年 usolv. All rights reserved.
//

#import "HNAPayMobileSDKManager.h"
#import "HNAPayDefaultDefine.h"
#import "HNAPayBaseNavigationController.h"
#import "HNAPayMainViewController.h"

typedef void(^Finish)(void);

@interface HNAPayMobileSDKManager ()

@property (copy ,nonatomic) Finish finish;

@end

@implementation HNAPayMobileSDKManager

- (instancetype)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedHNAPayClosedNotification:) name:NOTIFICATION_NAME_CLOSED object:nil];
    }
    return self;
}
-(void)startHNAPayWithParameters:(NSDictionary *)parameters finish:(void (^)(void))finishBlock{
     self.finish = finishBlock;
    
    HNAPayMainViewController *vc = [[HNAPayMainViewController alloc] init];
    HNAPayBaseNavigationController *nav = [[HNAPayBaseNavigationController alloc] initWithRootViewController:vc];
    vc.mDparameters = [parameters copy];
    nav.navigationBarHidden = YES;
    nav.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [[self topViewController] presentViewController:nav animated:YES completion:nil];
}
- (UIViewController*)topViewController
{
    UIViewController *debugVC = self.merViewController?self.merViewController:[self topViewControllerWithRootViewController:[[[[UIApplication sharedApplication] delegate] window] rootViewController]];
    NSLog(@"debugVC====%@",debugVC);
    return self.merViewController?self.merViewController:[self topViewControllerWithRootViewController:[[[[UIApplication sharedApplication] delegate] window] rootViewController]];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarController = (UITabBarController *)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}
-(void)receivedHNAPayClosedNotification:(NSNotification *)notification{
//    NSLog(@"%s",__func__);
    if (self.finish) {
        self.finish();
        NSHTTPCookie *cookie;
        NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
//        NSLog(@"关闭时：cookies===%@",[storage cookies]);
    }
}
-(void)dealloc
{
    self.finish = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
